﻿// Study0803.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 第八章 多态 重载运算符 非成员函数

#include <iostream>
using namespace std;

class Complex
{
public:
	Complex(int rr, int ii) :real(rr), imag(ii) {}
	Complex() :Complex(0, 0) {}
	friend Complex operator +(const Complex& c1, const Complex& c2);
	friend Complex operator -(const Complex& c1, const Complex& c2);
	friend ostream& operator <<(ostream& out, const Complex& c);
private:
	int real, imag;
};

Complex operator+(const Complex& c1, const Complex& c2)
{

	return { c1.real + c2.real,c1.imag + c2.imag };
}

Complex operator-(const Complex& c1, const Complex& c2)
{
	return { c1.real - c2.real,c1.imag - c2.imag };
}

ostream& operator <<(ostream& out, const Complex& c)
{
	out << "(" << c.real << "," << c.imag << ")";
	return out;
}

int main()
{
	Complex c1(5, 4), c2(2, 10);
	cout << "c1=";
	cout << c1 << endl;
	cout << "c2=";
	cout << c2 << endl;
	Complex c3 = c1 + c2;
	cout << "c3=c1+c2=";
	cout << c3 << endl;
	c3 = c1 - c2;
	cout << "c3=c1-c2=";
	cout << c3 << endl;
	return 0;
}


